from django.db import models
import time


# 职员表
class FaKuyouStaffMember(models.Model):
    STATUS_CHOICES = [
        ('0', '禁用'),
        ('1', '正常')
    ]

    id = models.AutoField(primary_key=True, verbose_name='ID')
    admin_id = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='关联管理员ID'
    )
    men_code = models.CharField(
        max_length=100,
        null=True,
        blank=True,
        verbose_name='职员编号',
        unique=True  # 确保职员编号唯一
    )
    name = models.CharField(
        max_length=100,
        null=True,
        blank=True,
        verbose_name='职员姓名'
    )
    phone = models.CharField(
        max_length=20,
        null=True,
        blank=True,
        verbose_name='联系电话'
    )
    status = models.CharField(
        max_length=1,
        choices=STATUS_CHOICES,
        default='1',
        verbose_name='职员状态'
    )
    createtime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='创建时间'
    )
    updatetime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='更新时间'
    )

    class Meta:
        db_table = 'fa_kuyou_staff_member'
        verbose_name = '职员信息'
        verbose_name_plural = '职员管理'
        indexes = [
            models.Index(fields=['men_code']),
            models.Index(fields=['name']),
            models.Index(fields=['status']),
        ]
        ordering = ['-createtime']

    def __str__(self):
        return f"{self.men_code or '无编号'}-{self.name or '新职员'}"

    def save(self, *args, **kwargs):
        # 自动维护时间戳
        now = int(time.time())
        if not self.createtime:
            self.createtime = now
        self.updatetime = now

        super().save(*args, **kwargs)
